package com.stkcall.library;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.telecom.PhoneAccountHandle;
import android.telecom.TelecomManager;

import androidx.annotation.RequiresApi;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>------------------------------------------------------
 * <p>Copyright (C) company, All rights reserved.
 * <p>------------------------------------------------------
 *
 * @author: xujie
 * @date: 7/16/22
 */
public class CallPhoneManager {
    @SuppressLint("StaticFieldLeak")
    private static CallPhoneManager INSTANCE;

    //监听列表
    private final ArrayList<CallStateListener> listeners = new ArrayList<>();
    private boolean isCall = false;//是否拨打
    private String mCurrentCallPhone;//当前拨打手机号

    public static synchronized CallPhoneManager getInstance(){
        synchronized (CallPhoneManager.class){
            if(INSTANCE == null) {

                INSTANCE = new CallPhoneManager();
            }
        }
        return INSTANCE;
    }
    private CallPhoneManager(){}

    public void init(Context context){

    }

    /**
     * 获取当前拨打手机号
     * @return
     */
    public String getCallPhone() {
        return mCurrentCallPhone;
    }

    /**
     * 获取当前是否拨打状态
     * @return
     */
    public boolean isCall() {
        return isCall;
    }

    /**
     * 重置拨打状态
     */
    public void resetIsCall() {
        isCall = false;
        mCurrentCallPhone = null;
    }

    /**
     * 拨打电话
     * @param context
     * @param phone 号码
     * @param iccid 指定卡
     */
    public void callNumber(Activity context, String phone,String iccid){

        //跳转拨打
        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
            TelecomManager telecomManager = null;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
                telecomManager = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
                if (telecomManager != null) {
                    @SuppressLint("MissingPermission")
                    List<PhoneAccountHandle> phoneAccountHandleList = telecomManager.getCallCapablePhoneAccounts();
                    PhoneAccountHandle phoneAccountHandle = null;
                    for (PhoneAccountHandle p :phoneAccountHandleList) {
                        if(iccid.equalsIgnoreCase(p.getId())){
                            phoneAccountHandle = p;
                        }
                    }
                    intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);

                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        context.startActivity(intent);

        //延时更新状态
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                updateState(context,CallState.CALL_OUT,phone,0);
                mCurrentCallPhone = phone;
                isCall = true;
            }
        },500);
    }

    /**
     * 添加监听
     * @param listener
     */
    public void addCallStateListener(CallStateListener listener){
        if(!listeners.contains(listener)){
            listeners.add(listener);
        }
    }

    /**
     * 移除监听
     * @param listener
     */
    public void removerCallStateListener(CallStateListener listener){
        listeners.remove(listener);
    }

    /**
     * 更新状态
     * @param context
     * @param state
     * @param phoneNumber
     */
    public void updateState(Context context, CallState state, String phoneNumber,long duration){
        for (CallStateListener l : listeners){
            l.CallState(context,state,phoneNumber,duration);
        }
    }

    /**
     * 清楚监听
     */
    public void clear(){
        listeners.clear();
    }
}
